﻿
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using UnityEditor;
using UnityEngine;

namespace zframe.editor
{
    /// <summary>
    /// 调用外部命令行工具
    /// </summary>
    public class ShellUtils
    {
        [MenuItem("zframe/ShellCmd/CallBat")]
        static void CallBat()
        {
            
            ZLogger.Log("EditorApplication.applicationPath="+ EditorApplication.applicationPath);
            string batDir = Path.Combine(Application.dataPath, "batRoot");
            string batFile = "mytest.bat";
         
            try {
                Process proc = new Process();
                proc.StartInfo.WorkingDirectory = batDir;    
                proc.StartInfo.FileName = batFile;
                //RedirectStandardInput为true  UseShellExecute必须为false
                proc.StartInfo.RedirectStandardInput = false;
                //proc.StartInfo.UseShellExecute = false;

                proc.EnableRaisingEvents = true; //抛出事件

                //设置是否显示窗口
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                proc.Exited += Process_Exited; 
                proc.Start();

            }
            catch (Exception e)
            {
                ZLogger.Log("CallBat Exception=" + e.Message);
            }
        }
        [MenuItem("zframe/ShellCmd/CallApp")]
        static void CallApp()
        {
            
            string batFile = Path.Combine(Application.dataPath, "batRoot", "app", "socketClientApp.exe");
            batFile = Path.Combine(Application.dataPath, "batRoot", "serverApp", "serverApp.exe");
            
            try
            {
                Process proc = new Process();
                //proc.StartInfo.WorkingDirectory = batDir;
                proc.StartInfo.FileName = batFile; //只需要设置FileName即可
                //RedirectStandardInput为true  UseShellExecute必须为false
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.UseShellExecute = false;

                proc.EnableRaisingEvents = true; //抛出事件

                //设置是否显示窗口
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                proc.Exited += Process_Exited;
                proc.OutputDataReceived += Proc_OutputDataReceived;

                proc.Start();
                proc.BeginOutputReadLine(); 

            }
            catch (Exception e)
            {
                ZLogger.Log("CallApp Exception=" + e.Message);
            }
        }

        

        private static void Proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            ZLogger.Log("Proc_OutputDataReceived e.Data=" + e.Data);
        }

        private static void Process_Exited(object sender, EventArgs e)
        {
            ZLogger.Log("Process_Exited e.Data=" + e);
            Process proc = sender as Process;
            if (true)
            {
                ZLogger.Log("Process_Exited 关闭进程.");
                proc.Close();
                
            }
        }
    }
}


